//========================================================================
//  SE5 Design Creation
//========================================================================
//  
//  Create AI Designs:
//  1. Is it time to create new designs?
//     a. Yes - New technology found
//     b. Yes - No designs at all
//     c. Yes - Every 10th turn
//  
//  2. For each design type
//     a. Get latest comps of each ability
//     b. Get possible components for this design
//     c. Get possible sizes for this design
//     d. Get weapon picks for this design
//     e. Is this design type needed?
//        1.  Yes - No current design of this type
//        2.  Yes - Current design version could be upgraded
//        3.  Yes - Majority Weapon has better selection
//        4.  Yes - Secondary Weapon has better selection
//        5.  Yes - Is there a larger size available than that used on Current version. 
//        6.  Yes - Newer engines available 
//        7.  Yes - Newer shields available
//        8.  Yes - Quantum Reactor now available
//        9.  No  - The current version was created recently.
//        10. No  - Must Have Components are not available. 
//     f. Create Design (see below)
//        1.  Add to the Vehicle Designs list. 
//        2.  Make old designs of this same type obsolete. 
//  
//  Create Design:
//  (when adding comps, pick the best comp enhancement for each)
//  1. Set Owner
//  2. Set AI Design Type
//  3. Set Design Class Name
//  4. Set Date Created
//  5. Set Obsolete = FALSE
//  6. Set Design Type
//  7. Get New Design Name
//  8. Pick a vehicle size
//  9. Satisfy Requirements 
//     a. Add Bridge
//     b. Add Life Support
//     c. Add Crew Quarters
//     d. Add Min Speed Engines
//     e. Add Fighter Bays
//     f. Add Colony Module
//     g. Add Cargo Bays
//  10. Add Must Have Components (1 each)
//  11. Add Engines to desired amount (desired speed or max engines)
//  12. Add Shield generators (to space factor)
//  13. Add Armor (to space factor)
//  14. Add Majority Component (to space factor)
//  15. Add Secondary Component (to space factor)
//  16. Add Misc Comps (to space factor)
//  17. If remaining space, add more Majority Components
//  18. If remaining space, add more Shields
//  19. If remaining space, add more armor
//  20. If remaining space, add basic armor
//  21. Set Automove and AutoTarget strategies
//  22. If AI Design Type = 0, then classify ship design. 


//------------------------------------------------------------------------
// Forward Declarations
//------------------------------------------------------------------------
deffunc

function Set_AI_Design_Type_For_Design returns boolean
params
  design_id:             long
end

function Create_Design returns boolean
params
  ai_design_type: string
  ai_design_type_index:    long
end

function Add_Components_To_Design returns boolean
params
  design_id:           long
  ai_design_type:      string
end

function Is_AI_Design_Type_Needed returns boolean
params
  ai_design_type: string
end

function Can_Create_Design_Type_Of_Type returns boolean
params
  ai_design_type:         string
  ai_design_type_index:   long
end

function Add_Components_To_Vehicle_Design returns boolean
params
  design_id:           long
  comp_id:             long 
  comp_enh_id:         long
  ship_section_id:     long
  num_comps_to_add:    long
  spread_from_bottom:  boolean
end

function Get_Best_Component_Of_Type returns long
params
  comp_type:      string
end

function Pick_Vehicle_Size returns long
params
  ai_design_type:         string
  ai_design_type_index:   long
end

function Get_Number_Of_Components_To_Add returns long
params
  design_id: long
  comp_type: string  
end

function Add_Required_Components_To_Design returns boolean
params
  design_id:              long
  ai_design_type:         string
end

function Mark_Old_Designs_Of_Type_Obsolete returns boolean
params
  design_id:        long
  ai_design_type:   string
end

function Get_Wanted_Component_From_List returns long
params
  lst_wanted_names:      stringlist
end

function Get_Vehicle_Size_Class_For_Size returns long
params
  size_name:             string
end

enddeffunc


//------------------------------------------------------------------------
// AI_DesignCreation
//------------------------------------------------------------------------
function AI_DesignCreation returns boolean
params
vars
  ai_design_type:              string
  ai_design_index:             long
  design_type_count:           long
  index:                       long
  design_count:                long
  design_list:                 longlist
  design_id:                   long
  lst_design_type_to_create:   stringlist
  lst_design_type_index:       longlist
begin
  return TRUE

  if (Sys_Using_AI_Minister(sys_long_Player_ID, "Vehicle Design")) then

    // Look for any designs that do not have an AI Design Type
    call Sys_Get_Vehicle_Designs_With_No_AI_Design_Type(sys_long_Player_ID, design_list)
    set design_count := design_list.count()
    if (design_count > 0) then
      for index := 1 to design_count do
        set design_id := design_list.get(index)
        if (not Sys_Is_Vehicle_Design_Obsolete(design_id)) then
          set ai_design_type := Sys_Get_Vehicle_Design_AI_Design_Type(design_id)
          if (Sys_Length_String(ai_design_type) = 0) then
            call Set_AI_Design_Type_For_Design(design_id)
          endif
        endif
      endfor
    endif

    set design_type_count := lst_AI_Design_Type_Name.count()
    if (design_type_count > 0) then
      for index := 1 to design_type_count do
        set ai_design_type := lst_AI_Design_Type_Name.Get(index)
        if (Is_AI_Design_Type_Needed(ai_design_type)) then
          if (Can_Create_Design_Type_Of_Type(ai_design_type, index)) then
            call lst_design_type_to_create.add(ai_design_type)
            call lst_design_type_index.add(index)  
          endif
        endif
      endfor
    endif

    set design_type_count := lst_design_type_to_create.count()
    if (design_type_count > 0) then
      call Sys_Prepare_For_Vehicle_Design_Creation(sys_long_Player_ID)
      
      for index := 1 to design_type_count do
        set ai_design_type := lst_design_type_to_create.get(index)
        set ai_design_index := lst_design_type_index.get(index)

        call Create_Design(ai_design_type, ai_design_index)
      endfor
    endif

  endif
end

//------------------------------------------------------------------------
// Create_Specific_Design (Called by Main_Create_Specific_Design)
//------------------------------------------------------------------------
function Create_Specific_Design returns boolean
params
  design_id:               long
vars
  new_design_name:         string
  bool_continue_design:    boolean
  ai_design_type:          string
  design_gen_type:         string
begin
  return FALSE

  call Sys_Prepare_For_Vehicle_Design_Creation(sys_long_Player_ID)

  // Convert the general type to a specific ai design type.
  set ai_design_type := ""
  set design_gen_type := Sys_Get_Vehicle_Design_General_Type(sys_long_Player_ID, design_id)
  
  case (design_gen_type) 
    "Attack Ship":
      set ai_design_type := "Attack Ship"
    "Defense Ship":
        set ai_design_type := "Attack Ship"
    "Explorer Ship":
        set ai_design_type := "Attack Ship"
    "Patrol Ship":
        set ai_design_type := "Attack Ship"
    "Scout Ship":
        set ai_design_type := "Attack Ship"
    "Colony (Rock)":
        set ai_design_type := "Colonizer (Rock)"
    "Colony (Ice)":
        set ai_design_type := "Colonizer (Ice)"
    "Colony (Gas)":
        set ai_design_type := "Colonizer (Gas)"
    "Attack Base":
        set ai_design_type := "Attack Base"
    "Defense Base":
        set ai_design_type := "Defense Base"
    "Base Space Yard":
        set ai_design_type := "Base Space Yard"
    "Base Repair Yard":
        set ai_design_type := "Base Repair Yard"
    "Space Yard Ship":
        set ai_design_type := "Space Yard Ship"
    "Repair Ship":
        set ai_design_type := "Repair Ship"
    "Population Transport":
        set ai_design_type := "Population Transport"
    "Troop Transport":
        set ai_design_type := "Troop Transport"
    "Cargo Transport":
        set ai_design_type := "Cargo Transport"
    "Remote Mining Ship":
        set ai_design_type := "Remote Mining Ship"
    "Remote Farming Ship":
        set ai_design_type := "Remote Farming Ship"
    "Remote Refining Ship":
        set ai_design_type := "Remote Refining Ship"
    "Remote Mining Base":
        set ai_design_type := "Remote Mining Base"
    "Remote Farming Base":
        set ai_design_type := "Remote Farming Base"
    "Remote Refining Base":
        set ai_design_type := "Remote Refining Base"
    "Carrier":
        set ai_design_type := "Carrier"
    "Mine Layer":
        set ai_design_type := "Mine Layer"
    "Mine Sweeper":
        set ai_design_type := "Mine Sweeper"
    "Boarding Ship":
        set ai_design_type := "Boarding Ship"
    "Fighter":
        set ai_design_type := "Fighter"
    "Mine":
        set ai_design_type := "Mine"
    "Satellite":
        set ai_design_type := "Satellite"
    "Recon Satellite":
        set ai_design_type := "Recon Satellite"
    "Troop":
        set ai_design_type := "Troop"
    "Weapons Platform":
        set ai_design_type := "Weapon Platform"
    "Anti-Ship Drone":
        set ai_design_type := "Anti-Ship Drone"
    "Anti-Planet Drone":
        set ai_design_type := "Anti-Planet Drone"
    "Drone Carrier":
        set ai_design_type := "Drone Carrier"
  endcase


  //  7. Get New Design Name
  set new_design_name := Sys_Get_New_Vehicle_Design_Name_Not_In_Use(sys_long_Player_ID)
  call Sys_Set_Vehicle_Design_Name(design_id, new_design_name)

  set bool_continue_design := Add_Required_Components_To_Design(design_id, ai_design_type)
  if (bool_continue_design) then
    set bool_continue_design := Add_Components_To_Design(design_id, ai_design_type)
  endif
end

//------------------------------------------------------------------------
// Set_AI_Design_Type_For_Design
//------------------------------------------------------------------------
function Set_AI_Design_Type_For_Design returns boolean
params
  design_id:             long
vars
  new_ai_design_type:  string
  size_id:             long
  size_name:           string
  vehicle_type:        string
  vehicle_size_class:  long
begin
  set new_ai_design_type := ""
  set size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set size_name := Sys_Get_Vehicle_Size_Name(size_id)  
  set vehicle_type := Sys_Get_Vehicle_Size_Vehicle_Type(size_id)

  case (vehicle_type)
    "Fighter":
      set new_ai_design_type := "Fighter"
    "Mine":
      set new_ai_design_type := "Mine"
    "Troop":
      set new_ai_design_type := "Troop"
    "Drone":
      set new_ai_design_type := "Anti-Ship Drone"
      // set new_ai_design_type := "Anti-Planet Drone"
    "Satellite":
      if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Basic Sensors")) then
        set new_ai_design_type := "Recon Satellite"
      else
        set new_ai_design_type := "Satellite"
      endif
    "Weapon Platform":
      set new_ai_design_type := "Weapon Platform"
    "Base":
      if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Space Yard")) then
        set new_ai_design_type := "Base Space Yard"
      else
        if (Sys_Get_Vehicle_Design_Space_Movement(design_id) > 0) then
          set new_ai_design_type := "Attack Base"
        else
          if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Robo - Miners")) or (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Robo - Farmers")) or (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Robo - Rad Extractors")) then
            set new_ai_design_type := "Resource Base"
          else
            set new_ai_design_type := "Defense Base"
          endif
        endif
      endif
    "Ship":
      set vehicle_size_class := Get_Vehicle_Size_Class_For_Size(size_name)
      case (vehicle_size_class)
        AI_VEHICLE_SIZE_CLASS_COLONY_SHIP:
          if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Rock Colony")) then
            set new_ai_design_type := "Colonizer (Rock)"
          endif
          if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Ice Colony")) then
            set new_ai_design_type := "Colonizer (Ice)"
          endif
          if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Gas Giant Colony")) then
            set new_ai_design_type := "Colonizer (Gas)"
          endif
        AI_VEHICLE_SIZE_CLASS_CARRIER:
          set new_ai_design_type := "Carrier"
        AI_VEHICLE_SIZE_CLASS_FREIGHTER:
          set new_ai_design_type := "Cargo Transport"
      endcase
      if (Sys_Length_String(new_ai_design_type) = 0) then
        if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Boarding Parties")) then
          set new_ai_design_type := "Boarding Ship"
        endif
        if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Drone Launcher")) then
          set new_ai_design_type := "Drone Carrier"
        endif
        if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Cobalt Warhead")) then
          set new_ai_design_type := "Kamikaze Attack Ship"
        endif
        if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Mine Layer")) then
          set new_ai_design_type := "Mine Layer"
        endif
        if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Mine Sweeper")) then
          set new_ai_design_type := "Mine Sweeper"
        endif
        if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Satellite Bay")) then
          set new_ai_design_type := "Satellite Layer"
        endif
        if (Sys_Length_String(new_ai_design_type) = 0) then
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Warp Point - Open Distance")) then  
            set new_ai_design_type := "Open Warp Point"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Warp Point - Close")) then
            set new_ai_design_type := "Close Warp Point"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Planet Create Size")) then
          set new_ai_design_type := "Create Planet"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Planet Destroy Size")) then
            set new_ai_design_type := "Destroy Planet"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Star - Create")) then
            set new_ai_design_type := "Create Star"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Star - Destroy")) then
            set new_ai_design_type := "Destroy Star"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Storm - Create")) then
            set new_ai_design_type := "Create Storm"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Storm - Destroy")) then
            set new_ai_design_type := "Destroy Storm"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Black Hole - Create")) then
            set new_ai_design_type := "Create Black Hole"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Black Hole - Destroy")) then
            set new_ai_design_type := "Destroy Black Hole"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Nebulae - Create")) then
            set new_ai_design_type := "Create Nebulae"
          endif
          if (Sys_Does_Vehicle_Design_Have_Ability_With_Name(sys_long_Player_ID, design_id, "Nebulae - Destroy")) then
            set new_ai_design_type := "Destroy Nebulae"
          endif
          if (Sys_Length_String(new_ai_design_type) = 0) then
            if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Space Yard")) then
              set new_ai_design_type := "Space Yard Ship"
            endif
          endif
          if (Sys_Length_String(new_ai_design_type) = 0) then
            if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Repair Bay")) then
              set new_ai_design_type := "Repair Ship"
            endif
          endif
          if (Sys_Length_String(new_ai_design_type) = 0) then
            if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Robo - Miners")) then
              set new_ai_design_type := "Remote Mining Ship"
            endif
            if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Robo - Farmers")) then
              set new_ai_design_type := "Remote Farming Ship"
            endif
            if (Sys_Does_Vehicle_Design_Have_Component_With_Name(design_id, "Robo - Rad Extractors")) then
              set new_ai_design_type := "Remote Refining Ship"
            endif
          endif
          if (Sys_Length_String(new_ai_design_type) = 0) then
            set new_ai_design_type := "Attack Ship"
          endif
        endif
      endif
  endcase

  if (Sys_Length_String(new_ai_design_type) > 0) then
    call Sys_Set_Vehicle_Design_AI_Design_Type(sys_long_Player_ID, design_id, new_ai_design_type)
  endif

  return TRUE
end

//------------------------------------------------------------------------
// Is_AI_Design_Type_Needed
//------------------------------------------------------------------------
function Is_AI_Design_Type_Needed returns boolean
params
  ai_design_type: string
vars
  retval: boolean
  old_design_id: long
  old_date: long
begin
  set retval := FALSE
  set old_design_id := Sys_Get_Latest_Vehicle_Design_Of_AI_Type(sys_long_Player_ID, ai_design_type)

  // Do we have any designs of this type?
  if (old_design_id = 0) then
    set retval := TRUE
  else
    // Has enough time passed that we should upgrade anyway?
    set old_date := Sys_Get_Vehicle_Design_Creation_Date(sys_long_Player_ID, old_design_id)
    if (sys_long_Game_Date - old_date < 5) then
      set retval := FALSE
    else
      // For our current design of this type, can it be upgraded?
      if (not retval) then
        if (Sys_Can_Vehicle_Design_Be_Upgraded(sys_long_Player_ID, old_design_id)) then
          set retval := TRUE
        endif
      endif
    endif
  endif

  return retval
end

//------------------------------------------------------------------------
// Can_Create_Design_Type_Of_Type
//------------------------------------------------------------------------
function Can_Create_Design_Type_Of_Type returns boolean
params
  ai_design_type:            string
  ai_design_type_index:      long
vars
  bool_continue_design:      boolean
  comp_name:                 string
  comp_abil:                 string
begin
  set bool_continue_design := TRUE

  if (Pick_Vehicle_Size(ai_design_type, ai_design_type_index) = 0) then
    set bool_continue_design := FALSE
  endif

  if (bool_continue_design) then
    set comp_name := ""
    set comp_abil := ""

    case (ai_design_type) 
      "Colonizer (Rock)":
        set comp_name := "Rock Colony"
      "Colonizer (Ice)":
        set comp_name := "Ice Colony"
      "Colonizer (Gas)":
        set comp_name := "Gas Giant Colony"
      "Weapon Platform":
        set comp_name := "Weapons Platform Computer Core"
      "Satellite":
        set comp_name := "Satellite Computer Core"
      "Mine":
        set comp_name := "Mine Warhead Standard"
      "Fighter":
        set comp_name := "Fighter Cockpit"
      "Troop":
        set comp_name := "Troop Cockpit"
      "Carrier":
        set comp_name := "Fighter Bay"
      "Troop Transport":
        set comp_name := "Cargo Bay"
      "Population Transport":
        set comp_name := "Cargo Bay"
      "Cargo Transport":
        set comp_name := "Large Supply Storage"
      "Anti-Ship Drone":
        set comp_name := "Drone Computer Core"
      "Anti-Planet Drone":
        set comp_name := "Drone Computer Core"
      "Boarding Ship":
        set comp_name := "Boarding Parties"
      "Drone Carrier":
        set comp_name := "Drone Launcher"
      "Kamikaze Attack Ship":
        set comp_name := "Cobalt Warhead"
      "Mine Layer":
        set comp_name := "Mine Layer"
      "Mine Sweeper":
        set comp_name := "Mine Sweeper"
      "Satellite Layer":
        set comp_name := "Satellite Bay"
      "Recon Satellite":
        set comp_name := "Basic Sensors"
      "Open Warp Point":
        set comp_abil := "Warp Point - Open Distance"
      "Close Warp Point":
        set comp_abil := "Warp Point - Close"
      "Create Planet":
        set comp_abil := "Planet Create Size"
      "Destroy Planet":
        set comp_abil := "Planet Destroy Size"
      "Create Star":
        set comp_abil := "Star - Create"
      "Destroy Star":
        set comp_abil := "Star - Destroy"
      "Create Storm":
        set comp_abil := "Storm - Create"
      "Destroy Storm":
        set comp_abil := "Storm - Destroy"
      "Create Black Hole":
        set comp_abil := "Black Hole - Create"
      "Destroy Black Hole":
        set comp_abil := "Black Hole - Destroy"
      "Create Nebulae":
        set comp_abil := "Nebulae - Create"
      "Destroy Nebulae":
        set comp_abil := "Nebulae - Destroy"
      "Base Space Yard":
        set comp_name := "Space Yard"
      "Base Repair Yard":
        set comp_name := "Repair Bay"
      "Space Yard Ship":
        set comp_name := "Space Yard"
      "Repair Ship":
        set comp_name := "Repair Bay"
      "Remote Mining Ship":
        set comp_name := "Robo - Miners"
      "Remote Farming Ship":
        set comp_name := "Robo - Farmers"
      "Remote Refining Ship":
        set comp_name := "Robo - Rad Extractors"
      "Remote Mining Base":
        set comp_name := "Robo - Miners"
      "Remote Farming Base":
        set comp_name := "Robo - Farmers"
      "Remote Refining Base":
        set comp_name := "Robo - Rad Extractors"
    endcase

    if (Sys_Length_String(comp_name) > 0) then
      if (Sys_Get_Component_With_Name(sys_long_Player_ID, comp_name) = 0) then
        set bool_continue_design := FALSE
      endif 
    endif
    if (Sys_Length_String(comp_abil) > 0) then
      if (Sys_Get_Best_Component_With_Ability(sys_long_Player_ID, comp_abil) = 0) then
        set bool_continue_design := FALSE
      endif 
    endif

  endif

  return bool_continue_design
end

//------------------------------------------------------------------------
// Create_Design
//------------------------------------------------------------------------
function Create_Design returns boolean
params
  ai_design_type:          string
  ai_design_type_index:    long
vars
  design_id:               long
  choose_size:             long
  new_design_name:         string
  bool_name_ok:            boolean
  bool_continue_design:    boolean
  bool_design_created:     boolean
begin
  return FALSE
  //  1. Set Owner
  set design_id := Sys_Create_Temporary_Vehicle_Design(sys_long_Player_ID)

  //  2. Set AI Design Type
  call Sys_Set_Vehicle_Design_Ai_Design_Type(sys_long_Player_ID, design_id, ai_design_type)

  //  3. Set Design Class Name
  call Sys_Set_Vehicle_Design_Class_Name(design_id, ai_design_type)

  //  5. Set Obsolete = FALSE
  call Sys_Set_Vehicle_Design_Obsolete(design_id, FALSE)

  //  6. Set Design Type
  call Sys_Set_Vehicle_Design_General_Type(design_id, ai_design_type)

  //  7. Get New Design Name
  set new_design_name := Sys_Get_New_Vehicle_Design_Name_Not_In_Use(sys_long_Player_ID)
  call Sys_Set_Vehicle_Design_Name(design_id, new_design_name)

  //  8. Pick a vehicle size
  set choose_size := Pick_Vehicle_Size(ai_design_type, ai_design_type_index)

  if (choose_size > 0) then 
    call Sys_Set_Vehicle_Design_Size_ID(sys_long_Player_ID, design_id, choose_size)
    set bool_continue_design := Add_Required_Components_To_Design(design_id, ai_design_type)
    if (bool_continue_design) then
      set bool_continue_design := Add_Components_To_Design(design_id, ai_design_type)
    endif
    if (bool_continue_design) then
      set design_id := Sys_Create_Permanent_Vehicle_Design(sys_long_Player_ID, design_id)
      if (design_id > 0) then
        call Mark_Old_Designs_Of_Type_Obsolete(design_id, ai_design_type)
        return TRUE
      endif
    endif
  endif  
end

//------------------------------------------------------------------------
// Add_Components_To_Design
//------------------------------------------------------------------------
function Add_Components_To_Design returns boolean
params
  design_id:           long
  ai_design_type:      string
vars
  comp_id:             long
  comp_enh_id:         long
  num_to_add:          long
  size_id:             long
  vehicle_tonnage:     long
  comp_tonnage:        long
  list_count:          long
  index:               long
  design_type_str:     string
  continue_design:     boolean
  found_type:          boolean
  pct_of_space:        long
begin
  set continue_design := TRUE
  set size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set vehicle_tonnage := Sys_Get_Vehicle_Size_Tonnage_Space(sys_long_Player_ID, size_id)
  set comp_enh_id := 0

  // Shields
  if (continue_design) then
    set found_type := FALSE

    if (ai_design_type = "Fighter") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Small Shields")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Fighter_Pct_Shields / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Troop") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Small Shields")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Troop_Pct_Shields / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Satellite") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Shields")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Satellite_Pct_Shields / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Weapon Platform") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Shields")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_WeapPlatform_Pct_Shields / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Anti-Ship Drone") or (ai_design_type = "Anti-Planet Drone") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Shields")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Drone_Pct_Shields / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif

    if (not found_type) then
      set comp_id := Get_Best_Component_Of_Type("Shields")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Ship_Pct_Shields / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif
  endif
  
  // Armor
  if (continue_design) then
    set found_type := FALSE

    if (ai_design_type = "Fighter") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Small Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Fighter_Pct_Armor / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Troop") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Small Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Troop_Pct_Armor / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Satellite") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Satellite_Pct_Armor / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Weapon Platform") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_WeapPlatform_Pct_Armor / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, FALSE)
      endif 
    endif

    if (ai_design_type = "Anti-Ship Drone") or (ai_design_type = "Anti-Planet Drone") then
      set found_type := TRUE
      set comp_id := Get_Best_Component_Of_Type("Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Drone_Pct_Armor / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, FALSE)
      endif 
    endif

    if (not found_type) then
      set comp_id := Get_Best_Component_Of_Type("Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Ship_Pct_Armor / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, num_to_add, FALSE)
      endif 
    endif
  endif
  
  // Primary Weapon
  if (continue_design) then
    if (ai_design_type = "Fighter") then
      set comp_id := Get_Best_Component_Of_Type("Small Primary Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Fighter_Pct_Primary_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else 
        set continue_design := FALSE
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Troop") then
      set comp_id := Get_Best_Component_Of_Type("Small Primary Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Troop_Pct_Primary_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else 
        set continue_design := FALSE
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Satellite") then
      set comp_id := Get_Best_Component_Of_Type("Satellite Primary Weapon")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Satellite_Pct_Primary_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else 
        set continue_design := FALSE
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Weapon Platform") then
      set comp_id := Get_Best_Component_Of_Type("Weapon Platform Primary Weapon")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_WeapPlatform_Pct_Primary_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else 
        set continue_design := FALSE
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Anti-Ship Drone") or (ai_design_type = "Anti-Planet Drone") then
      set comp_id := Get_Best_Component_Of_Type("Drone Primary Weapon")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Drone_Pct_Primary_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else 
        set continue_design := FALSE
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Attack Ship") or (ai_design_type = "Attack Base") or (ai_design_type = "Defense Base") then
      set comp_id := Get_Best_Component_Of_Type("Primary Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Ship_Pct_Primary_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else 
        set continue_design := FALSE
      endif 
    endif
  endif
  
  // Heavy Weapon
  if (continue_design) then
    if (ai_design_type = "Fighter") then
      set comp_id := Get_Best_Component_Of_Type("Small Heavy Weapon")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Fighter_Pct_Heavy_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Troop") then
      set comp_id := Get_Best_Component_Of_Type("Small Heavy Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Troop_Pct_Heavy_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Attack Ship") or (ai_design_type = "Attack Base") or (ai_design_type = "Defense Base") then
      set comp_id := Get_Best_Component_Of_Type("Heavy Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Ship_Pct_Heavy_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif
  endif

  // Special Weapon
  if (continue_design) then
    if (ai_design_type = "Weapon Platform") then
      set comp_id := Get_Best_Component_Of_Type("Weapon Platform Special Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_WeapPlatform_Pct_Special_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif
  endif

  if (continue_design) then
    if (ai_design_type = "Attack Ship") or (ai_design_type = "Attack Base") or (ai_design_type = "Defense Base") then
      set comp_id := Get_Best_Component_Of_Type("Special Weapon")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc((vehicle_tonnage * (lng_AI_Design_Tonnage_Ship_Pct_Special_Weapon / 100)) / comp_tonnage)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      endif 
    endif
  endif
  
  //  16. Add Misc Comps (to space factor)
  if (continue_design) and (Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id) > 0) then
    set found_type := FALSE
    if (ai_design_type = "Fighter") or (ai_design_type = "Troop") then
      set found_type := TRUE
      set list_count := lst_Extra_Comps_Small_Name.count()
      if (list_count > 0) then
        for index := 1 to list_count do
          set design_type_str := lst_Extra_Comps_Small_Only_For_Design_Type.Get(index)
          if (design_type_str = "") or (design_type_str = ai_design_type) then
            set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, lst_Extra_Comps_Small_Name.Get(index))
            if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
              set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
              set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
              set pct_of_space := lst_Extra_Comps_Small_Pct_Of_Space.get(index)
              set num_to_add := 0
              if (pct_of_space < 0) then
                set num_to_add := -pct_of_space
              else
                set num_to_add := Sys_Trunc((vehicle_tonnage * (pct_of_space / 100)) / comp_tonnage)
              endif 

              if (num_to_add > 0) then
                call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
              endif
            endif 
          endif  
        endfor
      endif
    endif 

    if (not found_type) then
      set list_count := lst_Extra_Comps_Name.count()
      if (list_count > 0) then
        for index := 1 to list_count do
          set design_type_str := lst_Extra_Comps_Only_For_Design_Type.Get(index)
          if (design_type_str = "") or (design_type_str = ai_design_type) then
            set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, lst_Extra_Comps_Name.Get(index))
            if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
              set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
              set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    

              set pct_of_space := lst_Extra_Comps_Pct_Of_Space.get(index)
              set num_to_add := 0
              if (pct_of_space < 0) then
                set num_to_add := -pct_of_space
              else
                set num_to_add := Sys_Trunc((vehicle_tonnage * (pct_of_space / 100)) / comp_tonnage)
              endif 

              if (num_to_add > 0) then
                call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
              endif

            endif 
          endif  
        endfor
      endif
    endif

  endif

  //  17. If remaining space, add more Majority Components
  //  18. If remaining space, add more Shields
  //  19. If remaining space, add more armor

  if (continue_design) and (Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id) > 0) then
    if (ai_design_type = "Fighter") or (ai_design_type = "Troop") then
      set comp_id := Get_Best_Component_Of_Type("Small Armor")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, 100, FALSE)
      endif 
    else
      set comp_id := Get_Best_Component_Of_Type("Armor")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_ARMOR, 100, FALSE)
      endif 
    endif
  endif
  
  //  21. Set Automove and AutoTarget strategies

  return continue_design
end

//------------------------------------------------------------------------
// Add_Components_To_Vehicle_Design
//------------------------------------------------------------------------
function Add_Components_To_Vehicle_Design returns boolean
params
  design_id:           long
  comp_id:             long 
  comp_enh_id:         long
  ship_section_id:     long
  num_comps_to_add:    long
  spread_from_bottom:  boolean
vars
  current_tonnage:     long
  total_tonnage:       long
  comp_tonnage:        long
  index:               long
  remaining_tonnage:   long
  comp_tonnage:        long
  comp_spread_type:    long
  comp_spread_id:      long
  retval:              boolean
begin
  set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
  set comp_spread_type := COMP_SPREAD_TYPE_DIRECTION
  set comp_spread_id := COMP_SPREAD_ID_DIR_TOP_DOWN
  if (spread_from_bottom) then
    set comp_spread_id := COMP_SPREAD_ID_DIR_BOTTOM_UP
  endif

  if (num_comps_to_add > 0) then
    set index := 0
    loop
      set index := index + 1
      set remaining_tonnage := Sys_Get_Vehicle_Design_Remaining_Tonnage_Space(design_id)
      set retval := FALSE
      if (remaining_tonnage >= comp_tonnage) then
        set retval := Sys_Add_Component_To_Vehicle_Design(sys_long_Player_ID, design_id, comp_id, comp_enh_id, ship_section_id, comp_spread_type, comp_spread_id)
      endif
      exitwhen ((index >= num_comps_to_add) or (not retval))
    endloop

  endif  
end

//------------------------------------------------------------------------
// Get_Best_Component_Of_Type
//------------------------------------------------------------------------
function Get_Best_Component_Of_Type returns long
params
  comp_type:      string
vars
  comp_id:        long
  final_comp_id:  long
begin
  set final_comp_id := 0

  case (comp_type) 

    "Engine":
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Quantum Engine")  
      if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
        set final_comp_id := comp_id
      endif 
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Jacketed - Photon Engine")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif  
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Contra - Terrene Engine")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif  
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Ion Engine")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif

    "Small Engine":    
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Quantum Engine")  
      if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
        set final_comp_id := comp_id
      endif 
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Jacketed - Photon Engine")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif  
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Contra - Terrene Engine")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif  
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Ion Engine")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif

    "Ground Movement":
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Quantum Ground Thrusters")  
      if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
        set final_comp_id := comp_id
      endif 
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Anti-Grav Ground Thrusters")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif  
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Fusion Ground Thrusters")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif  
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Nuclear Ground Thrusters")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif

    "Primary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Weapon_Name)

    "Small Primary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Weapon_Small_Name)

    "Satellite Primary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Weapon_Name)

    "Drone Primary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Weapon_Name)

    "Weapon Platform Primary Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Primary_Weapon_Name)

    "Heavy Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Heavy_Weapon_Name)

    "Small Heavy Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Heavy_Weapon_Small_Name)

    "Special Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Special_Weapon_Name)

    "Weapon Platform Special Weapon":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Special_Weapon_WeapPlatform_Name)

    "Shields":
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Phased - Shield Generator")  
      if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
        set final_comp_id := comp_id
      endif
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Shield Generator")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif

    "Small Shields":
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Shield Generator")  
      if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
        set final_comp_id := comp_id
      endif

    "Armor":
      set final_comp_id := Get_Wanted_Component_From_List(lst_Armor_Wanted_Name)

    "Small Armor":
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Emissive Armor")  
      if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
        set final_comp_id := comp_id
      endif
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Small Armor")  
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif
  endcase

  return final_comp_id 
end

//------------------------------------------------------------------------
// Get_Wanted_Component_From_List
//------------------------------------------------------------------------
function Get_Wanted_Component_From_List returns long
params
  lst_wanted_names:      stringlist
vars
  comp_id:        long
  final_comp_id:  long
  list_count:     long
  index:          long
begin
  set final_comp_id := 0

  set list_count := lst_wanted_names.Count()
  if (list_count > 0) then
    for index := 1 to list_count do
      if (final_comp_id = 0) then
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, lst_wanted_names.Get(index))
        if (Sys_Is_Component_Available(sys_long_Player_ID, comp_id)) then
          set final_comp_id := comp_id
        endif 
      endif
    endfor
  endif
  return final_comp_id
end

//------------------------------------------------------------------------
// Pick_Vehicle_Size
//------------------------------------------------------------------------
function Pick_Vehicle_Size returns long
params
  ai_design_type:          string
  ai_design_type_index:    long
vars
  size_id:                 long
  vehicle_size_class:      long
  vehicle_size_name:       string
  index:                   long
  num_items:               long
begin
  return 0 

  set vehicle_size_class := lst_AI_Design_Type_Vehicle_Size.Get(ai_design_type_index)

  set num_items := lst_AI_Vehicle_Sizes_Name.count()
  if (num_items > 0) then
    for index := 1 to num_items do
      if (lst_AI_Vehicle_Sizes_AI_Size_Class.Get(index) = vehicle_size_class) then
        set vehicle_size_name := lst_AI_Vehicle_Sizes_Name.Get(index)

        set size_id := Sys_Get_Vehicle_Size_From_Name(vehicle_size_name)
        if (size_id > 0) then
          if (Sys_Is_Vehicle_Size_Available(sys_long_Player_ID, size_id)) then  
            return size_id
          endif
        endif

      endif
    endfor
  endif

end

//------------------------------------------------------------------------
// Get_Number_Of_Components_To_Add
//------------------------------------------------------------------------
function Get_Number_Of_Components_To_Add returns long
params
  design_id: long
  comp_type: string  
vars
  ship_size_id: long
  ship_size_name: string
  index: long
  num_items: long
begin 
  return 1
  set ship_size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set ship_size_name := Sys_Get_Vehicle_Size_Name(ship_size_id)

  set num_items := lst_AI_Vehicle_Sizes_Name.count()
  if (num_items > 0) then
    set index := 0
    loop
      set index := index + 1
      if (lst_AI_Vehicle_Sizes_Name.Get(index) = ship_size_name) then
        case (comp_type)
          "Life Support":
            return lst_AI_Vehicle_Sizes_Num_Life_Support.Get(index)
          "Crew Quarters":
            return lst_AI_Vehicle_Sizes_Num_Crew_Quarters.Get(index)
          "Engine":
            return lst_AI_Vehicle_Sizes_Num_Engines.Get(index)
          "Small Engine":
            return lst_AI_Vehicle_Sizes_Num_Engines.Get(index)
          "Ground Movement":
            return lst_AI_Vehicle_Sizes_Num_Engines.Get(index)
        endcase
        exitwhen (TRUE)
      endif
      exitwhen (index >= num_items)
    endloop
  endif

end


//------------------------------------------------------------------------
// Add_Required_Components_To_Design
//------------------------------------------------------------------------
function Add_Required_Components_To_Design returns boolean
params
  design_id:              long
  ai_design_type:         string
vars
  num_to_add:             long
  comp_id:                long
  size_name:              string
  size_id:                long
  vehicle_tonnage:        long
  vehicle_size_class:     long
  comp_tonnage:           long
  bool_continue_design:   boolean
  vehicle_type:           string
  comp_name:              string
  pct_space:              long
  comp_abil:              string
  comp_enh_id:            long
  add_only_one:           boolean
begin 
  set bool_continue_design := TRUE

  //  10. Add Must Have Components (1 each)
  //  11. Add Engines to desired amount (desired speed or max engines)

  set size_id := Sys_Get_Vehicle_Design_Size_ID(design_id)
  set vehicle_tonnage := Sys_Get_Vehicle_Size_Tonnage_Space(sys_long_Player_ID, size_id)
  set size_name := Sys_Get_Vehicle_Size_Name(size_id)  
  set vehicle_type := Sys_Get_Vehicle_Size_Vehicle_Type(size_id)
  set vehicle_size_class := Get_Vehicle_Size_Class_For_Size(size_name)

  if (Sys_Is_Vehicle_Size_Ship(size_id) or Sys_Is_Vehicle_Size_Base(size_id)) then
    // Add required components for Ships and Bases
    //  9. Satisfy Requirements 
    //     a. Add Bridge
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Bridge")  
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
    else
      set bool_continue_design := FALSE
    endif 

    //     b. Add Life Support (Multiple)
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Life Support")  
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set num_to_add := Get_Number_Of_Components_To_Add(design_id, "Life Support")
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, num_to_add, FALSE)
    else
      set bool_continue_design := FALSE
    endif 

    //     c. Add Crew Quarters (Multiple)
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Crew Quarters")  
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set num_to_add := Get_Number_Of_Components_To_Add(design_id, "Crew Quarters")
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, num_to_add, FALSE)
    else
      set bool_continue_design := FALSE
    endif 

    //     d. Add Basic Sensors
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Basic Sensors")  
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
    endif 
  else
    // Add required components for Units
    if (vehicle_type = "Fighter") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Fighter Cockpit")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Fighter Life Support")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
    if (vehicle_type = "Troop") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Troop Cockpit")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
    if (vehicle_type = "Satellite") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Satellite Computer Core")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
    if (vehicle_type = "Weapon Platform") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Weapons Platform Computer Core")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
    if (vehicle_type = "Drone") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Drone Computer Core")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_INNER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
  endif
 
  if (Sys_Is_Vehicle_Size_Ship(size_id)) then
    //     e. Add Min Speed Engines
    set comp_id := Get_Best_Component_Of_Type("Engine")  
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set num_to_add := Sys_Min_Long(Get_Number_Of_Components_To_Add(design_id, "Engine"), lng_AI_Design_Minimum_Speed_Ship)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, TRUE)
    endif 
  else
    if (vehicle_type = "Fighter") then
      set comp_id := Get_Best_Component_Of_Type("Small Engine")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Sys_Min_Long(Get_Number_Of_Components_To_Add(design_id, "Small Engine"), lng_AI_Design_Minimum_Speed_Fighter)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, TRUE)
      endif 
    endif
    if (vehicle_type = "Troop") then
      set comp_id := Get_Best_Component_Of_Type("Ground Movement")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Sys_Min_Long(Get_Number_Of_Components_To_Add(design_id, "Ground Movement"), lng_AI_Design_Minimum_Speed_Troop)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, TRUE)
      endif 
    endif
    if (vehicle_type = "Drone") then
      set comp_id := Get_Best_Component_Of_Type("Engine")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set num_to_add := Sys_Min_Long(Get_Number_Of_Components_To_Add(design_id, "Engine"), lng_AI_Design_Minimum_Speed_Drone)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, TRUE)
      endif 
    endif
  endif

  //     f. Add Colony Module
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_COLONY_SHIP) then
    if (ai_design_type = "Colonizer (Rock)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Rock Colony")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
    if (ai_design_type = "Colonizer (Ice)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Ice Colony")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
    if (ai_design_type = "Colonizer (Gas)") then
      set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Gas Giant Colony")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, 1, FALSE)
      else
        set bool_continue_design := FALSE
      endif 
    endif
  endif

  //     g. Add Cargo Bays or Supply/Ordnance Storage for Cargo Transports
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Troop Transport") or (ai_design_type = "Population Transport") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Cargo Bay")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.4) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
     endif
  endif
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Cargo Transport") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Large Supply Storage")  
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.20) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Large Ordnance Storage")
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.20) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif

  //     g. Add Remote Resource Components for Remote Mining Freighters etc.
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Remote Mining Ship") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Robo - Miners")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.40) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Remote Farming Ship") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Robo - Farmers")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.40) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Remote Refining Ship") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Robo - Rad Extractors")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.40) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif

  //     g. Add Remote Resource Components for Remote Mining Bases etc.
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Remote Mining Ship") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Robo - Miners")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.40) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Remote Farming Ship") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Robo - Farmers")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.40) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_FREIGHTER) then
    if (ai_design_type = "Remote Refining Ship") then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Robo - Rad Extractors")
      if (comp_id > 0) then
        set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
        set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)
        set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.40) / comp_tonnage) + 0.99)
        call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
      else
        set bool_continue_design := FALSE
      endif
    endif
  endif

  //     g. Add Fighter Bays
  if (vehicle_size_class = AI_VEHICLE_SIZE_CLASS_CARRIER) then
    set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, "Fighter Bay")  
    if (comp_id > 0) then
      set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
      set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
      set num_to_add := Sys_Trunc(((vehicle_tonnage * 0.4) / comp_tonnage) + 0.99)
      call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
    else
      set bool_continue_design := FALSE
    endif 
  endif 

  if (bool_continue_design) then
    set comp_name := ""
    set pct_space := 0
    set comp_abil := ""
    set add_only_one := FALSE
    case (ai_design_type) 
      "Boarding Ship":
        set comp_name := "Boarding Parties"
        set pct_space := 40
      "Drone Carrier":
        set comp_name := "Drone Launcher"
        set pct_space := 40
      "Kamikaze Attack Ship":
        set comp_name := "Cobalt Warhead"
        set pct_space := 40
      "Mine":
        set comp_name := "Mine Warhead Standard"
        set pct_space := 100
      "Mine Layer":
        set comp_name := "Mine Layer"
        set pct_space := 40
      "Mine Sweeper":
        set comp_name := "Mine Sweeper"
        set pct_space := 40
      "Satellite Layer":
        set comp_name := "Satellite Bay"
        set pct_space := 40
      "Recon Satellite":
        set comp_name := "Basic Sensors"
        set add_only_one := TRUE
      "Open Warp Point":
        set comp_abil := "Warp Point - Open Distance"
        set add_only_one := TRUE
      "Close Warp Point":
        set comp_abil := "Warp Point - Close"
        set add_only_one := TRUE
      "Create Planet":
        set comp_abil := "Planet Create Size"
        set add_only_one := TRUE
      "Destroy Planet":
        set comp_abil := "Planet Destroy Size"
        set add_only_one := TRUE
      "Create Star":
        set comp_abil := "Star - Create"
        set add_only_one := TRUE
      "Destroy Star":
        set comp_abil := "Star - Destroy"
        set add_only_one := TRUE
      "Create Storm":
        set comp_abil := "Storm - Create"
        set add_only_one := TRUE
      "Destroy Storm":
        set comp_abil := "Storm - Destroy"
        set add_only_one := TRUE
      "Create Black Hole":
        set comp_abil := "Black Hole - Create"
        set add_only_one := TRUE
      "Destroy Black Hole":
        set comp_abil := "Black Hole - Destroy"
        set add_only_one := TRUE
      "Create Nebulae":
        set comp_abil := "Nebulae - Create"
        set add_only_one := TRUE
      "Destroy Nebulae":
        set comp_abil := "Nebulae - Destroy"
        set add_only_one := TRUE
      "Base Space Yard":
        set comp_abil := "Space Yard"
        set add_only_one := TRUE
      "Space Yard Ship":
        set comp_abil := "Space Yard"
        set add_only_one := TRUE
      "Base Repair Yard":
        set comp_name := "Repair Bay"
        set pct_space := 40
      "Repair Ship":
        set comp_name := "Repair Bay"
        set pct_space := 40
      "Remote Mining Ship":
        set comp_name := "Robo - Miners"
        set pct_space := 40
      "Remote Farming Ship":
        set comp_name := "Robo - Farmers"
        set pct_space := 40
      "Remote Refining Ship":
        set comp_name := "Robo - Rad Extractors"
        set pct_space := 40
      "Remote Mining Base":
        set comp_name := "Robo - Miners"
        set pct_space := 40
      "Remote Farming Base":
        set comp_name := "Robo - Farmers"
        set pct_space := 40
      "Remote Refining Base":
        set comp_name := "Robo - Rad Extractors"
        set pct_space := 40
    endcase
    if (add_only_one) then
      set bool_continue_design := FALSE
      set comp_id := Sys_Get_Best_Component_With_Ability(sys_long_Player_ID, comp_abil)  
      if (comp_id > 0) then
        call Add_Components_To_Vehicle_Design(design_id, comp_id, 0, SHIP_SECTION_OUTER_HULL, 1, FALSE)
        set bool_continue_design := TRUE
      endif 
    else
      if (pct_space > 0) then
        set bool_continue_design := FALSE
        set comp_id := Sys_Get_Component_With_Name(sys_long_Player_ID, comp_name)  
        if (comp_id > 0) then
          set comp_enh_id := Sys_Get_Best_Comp_Enh_For_Component(sys_long_Player_ID, design_id, comp_id)
          set comp_tonnage := Sys_Get_Component_Tonnage_Space(sys_long_Player_ID, comp_id, comp_enh_id)    
          set num_to_add := Sys_Trunc(((vehicle_tonnage * (pct_space / 100)) / comp_tonnage) + 0.99)
          call Add_Components_To_Vehicle_Design(design_id, comp_id, comp_enh_id, SHIP_SECTION_OUTER_HULL, num_to_add, FALSE)
          set bool_continue_design := TRUE
        endif 
      endif
    endif
  endif

  return bool_continue_design
end

//------------------------------------------------------------------------
// Mark_Old_Designs_Of_Type_Obsolete
//------------------------------------------------------------------------
function Mark_Old_Designs_Of_Type_Obsolete returns boolean
params
  design_id:             long
  ai_design_type:        string
vars
  design_count:          long
  index:                 long
  this_ai_design_type:   string
  this_design_id:        long
begin
  return FALSE
  set design_count := Sys_Get_Player_Designs_Count(sys_long_Player_ID)
  if (design_count > 0) then
    for index := 1 to design_count do
      set this_design_id := Sys_Get_Player_Design_ID(sys_long_Player_ID, index)
      if (this_design_id > 0) and (this_design_id <> design_id) then
        set this_ai_design_type := Sys_Get_Vehicle_Design_AI_Design_Type(this_design_id)
        if (this_ai_design_type = ai_design_type) then
          call Sys_Mark_Vehicle_Design_Obsolete(this_design_id)
          return TRUE
        endif
      endif
    endfor
  endif
end


//------------------------------------------------------------------------
// Get_Vehicle_Size_Class_For_Size
//------------------------------------------------------------------------
function Get_Vehicle_Size_Class_For_Size returns long
params
  size_name:             string
vars
  vehicle_size_class:    long
  num_items:             long
  index:                 long
begin
  set vehicle_size_class := 0
  set num_items := lst_AI_Vehicle_Sizes_Name.count()
  set index := 0
  loop
    set index := index + 1
    if (lst_AI_Vehicle_Sizes_Name.Get(index) = size_name) then
      set vehicle_size_class := lst_AI_Vehicle_Sizes_AI_Size_Class.get(index)
    endif
    exitwhen (vehicle_size_class > 0) or (index >= num_items)
  endloop

  return vehicle_size_class
end


//------------------------------------------------------------------------